<template>
	<div>
		<Modal v-model="modal" :title="modalTitle" :mask-closable="false" @on-cancel="cancel">
			<Form ref="formValidate" :model="editData" :rules="ruleValidate" :label-width="100">
				<FormItem label="品牌名称" prop="brandName">
					<Input v-model.trim="editData.brandName" placeholder="请输入品牌名称"></Input>
				</FormItem>
			</Form>
			<div slot="footer">
				<Button @click="cancel">取消</Button>
				<Button type="primary" @click="submit" :loading="loadingBtn">
					<span v-if="!loadingBtn">确定</span>
					<span v-else>请稍候...</span>
				</Button>
			</div>
		</Modal>
	</div>
</template>

<script>
export default {
    components:{
    },
	props:{
	    type:String
	},
    data(){
        return {
            modal:false,
            modalTitle:'',
            editData:{
                brandName:''
            },
            brandId:0,
            isEdit:false,
            ruleValidate:{
                brandName:{required:true,message:'请输入品牌名称'}
            },
            loadingBtn:false
        }
    },
    methods:{  
        show(res){
            this.$nextTick(()=>{
                this.modal = true
				 // 根据类型判断显示对话框的标题
                switch (this.type) {
                    case 'add' :
                    this.isEdit = false
                    this.modalTitle = '新增品牌'
                    break
                    case 'edit':
                    this.isEdit = true
					this.modalTitle = '编辑品牌'
                    this.brandId = res.row.brandId
					this.editData.brandName = res.row.brandName
                    break
                }
            })
            
        },
        
        //取消
        cancel(){
			this.modal = false;
        },
        //提交
        submit(){
            this.$refs['formValidate'].validate((valid) => {
                if(valid){
                    let data = {
                        brandName:this.editData.brandName
                    }
                    if(this.isEdit){
                        data.brandId = this.brandId
                    }
					let url = '/partsbrand/save'
                    this.$http.post(url,data).then(res=>{
                        if(res.status==200){
							if(res.data.code==200){
								 this.$Message.success('操作成功')
								this.modal = false
								this.$emit('table')
							}
                           
                        }
                    })
                }
                
            })
            
        },
        modalChange(state){
            if(state){

            }else{
                this.$refs.formValidate.resetFields()
            }
        }
    }
}
</script>
<style scoped>
.uploadSuccess{
    width: 200px;
    height: 100px;
}
</style>
